//
//  DDFilteredProtocol.m
//  DDHybridEngine
//
//  Created by dtdream on 17/3/11.
//  Copyright © 2017年 dtdream. All rights reserved.
//

#import "DDFilteredProtocol.h"

static NSString * const URLProtocolHandledKey = @"URLProtocolHandledKey";

@interface DDFilteredProtocol ()<NSURLConnectionDelegate>

@property (nonatomic, strong) NSURLConnection *connection;

@end

@implementation DDFilteredProtocol

+ (BOOL)shouldCanonicalRequest:(NSURLRequest*)request {
    if ([request.URL host].length == 0) {
        return NO;
    }
    NSString *originUrlString = [request.URL absoluteString];
    NSString *originHostString = [request.URL host];
    NSRange hostRange = [originUrlString rangeOfString:originHostString];
    if (hostRange.location == NSNotFound) {
        return NO;
    }
    
    NSArray *urlArray = @[@"http://app.zjzwfw.gov.cn/client/jssdk/js/index.js",
                          @"http://app.zjzwfw.gov.cn/client/jssdk/require.js",
                          @"https://app.zjzwfw.gov.cn/client/jssdk/js/index.js",
                          @"http://app.zjzwfw.gov.cn/client/jssdkJS/writejs/index.js",
                          @"https://app.zjzwfw.gov.cn/client/jssdkJS/writejs/index.js"];
    
    for (NSString *url in urlArray) {
        if ([url isEqualToString:originUrlString]) {
            return YES;
        }
    }
    
    return NO;
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    return [self shouldCanonicalRequest:request];
}

+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
    NSLog(@"canonicalRequestForRequest :%@", request.URL);
    return [self redirectHostInRequset:request];
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b
{
    //NSLog(@"canonicalRequestForRequest :%@ toRequest:%@", a.URL, b.URL);
    return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)startLoading
{
    NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];
    //NSLog(@"startLoading :%@", mutableReqeust.URL);
    
    //打标签，防止无限循环
    [NSURLProtocol setProperty:@YES forKey:URLProtocolHandledKey inRequest:mutableReqeust];
    
    self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];
}

- (void)stopLoading
{
    [self.connection cancel];
}

#pragma mark - NSURLConnectionDelegate

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.client URLProtocol:self didLoadData:data];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.client URLProtocolDidFinishLoading:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.client URLProtocol:self didFailWithError:error];
}

#pragma mark -- private

+(NSURLRequest*)redirectHostInRequset:(NSURLRequest *)request
{
    NSLog(@"redirectHostInRequset :%@", request.URL);
    if ([request.URL host].length == 0) {
        return request;
    }
    
    NSString *originUrlString = [request.URL absoluteString];
    NSString *originHostString = [request.URL host];
    NSLog(@"redirectHostInRequset :%@ &&&&&&&originUrlString:%@", originUrlString, originHostString);
    NSRange hostRange = [originUrlString rangeOfString:originHostString];
    if (hostRange.location == NSNotFound) {
        return request;
    }
    
    if ([originUrlString isEqual:@"http://app.zjzwfw.gov.cn/client/jssdk/js/index.js"]) {
        NSMutableURLRequest *mutableReqeust = [request mutableCopy];
        //NSString *urlString = @"http://115.29.248.20/testjs/test.js";
        NSString *urlString = @"http://hzmetro.dtdream.com:8080/ddhybirdengine/test.js";
        NSURL *url = [NSURL URLWithString:urlString];
        mutableReqeust.URL = url;
        return mutableReqeust;
    } else if([originUrlString isEqual:@"http://app.zjzwfw.gov.cn/client/jssdk/require.js"]) {
        NSMutableURLRequest *mutableReqeust = [request mutableCopy];
        NSString *urlString = @"http://115.29.248.20/testjs/requirenew.js";
        NSURL *url = [NSURL URLWithString:urlString];
        mutableReqeust.URL = url;
        return mutableReqeust;
    } else if([originUrlString isEqual:@"https://app.zjzwfw.gov.cn/client/jssdk/js/index.js"]) {
        NSMutableURLRequest *mutableReqeust = [request mutableCopy];
        NSString *urlString = @"https://hzmetro.dtdream.com/ddhybirdengine/test.js";
        NSURL *url = [NSURL URLWithString:urlString];
        mutableReqeust.URL = url;
        return mutableReqeust;
    } else if([originUrlString isEqual:@"http://app.zjzwfw.gov.cn/client/jssdkJS/writejs/index.js"]) {
        NSMutableURLRequest *mutableReqeust = [request mutableCopy];
        NSString *urlString = @"http://hzmetro.dtdream.com:8080/ddhybirdengine/test.js";
        NSURL *url = [NSURL URLWithString:urlString];
        mutableReqeust.URL = url;
        return mutableReqeust;
    }
    
    
    //
    //    //定向到bing搜索主页
    //    NSString *ip = @"cn.bing.com";
    //
    //    // 替换host
    //    NSString *urlString = [originUrlString stringByReplacingCharactersInRange:hostRange withString:ip];
    //    NSURL *url = [NSURL URLWithString:urlString];
    //    request.URL = url;
    //
    //    NSLog(@"redirectHostInRequset end######:%@", request.URL);
    return request;
}

@end
